using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Thermometer_Drag : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler
{
    private GameObject m_DraggingIcon;
    private RectTransform m_DraggingPlane;
    public bool dragOnSurfaces = true;

    public Sprite Thermometer_OnTheTable;
    public Sprite Thermometer_PickUp;
    public Image myImage;

    public bool isFalling;
    public float fallingSpeed;
    public float stopFallingPositionY;

    public bool isUsingThermometer;
    
    public Vector3 mousePosition;
    public Transform ThermometerTestArea_BottomLeftCorner;
    public Transform ThermometerTestArea_TopRightCorner;
    public Transform ThermometerArea_BottomLeftCorner;
    public Transform ThermometerArea_TopRightCorner;
    
    public NPCManager nPCManager;


    
    private void Update()
    {
        mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        
        if (isUsingThermometer)
        {
            if (mousePosition.x <= ThermometerTestArea_TopRightCorner.position.x &&
                mousePosition.x >= ThermometerTestArea_BottomLeftCorner.position.x)
            {
                if (mousePosition.y <= ThermometerTestArea_TopRightCorner.position.y &&
                    mousePosition.y >= ThermometerTestArea_BottomLeftCorner.position.y)
                {
                    if (nPCManager.currentNPC != null)
                    {
                        myImage.sprite = nPCManager.currentNPC.GetComponent<NPCController>().NPCTemperature;
                        myImage.SetNativeSize();
                    }
                    else
                    {
                        Debug.Log("no NPC in the scene or not stand still.");
                    }
                }
                else
                {
                    myImage.sprite = Thermometer_PickUp;
                    myImage.SetNativeSize();
                }
            }
            else
            {
                myImage.sprite = Thermometer_PickUp;
                myImage.SetNativeSize();
            }
        }
        
        if (isFalling)
        {
            FreeFallToTheTable();
        }
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        if (NPCManager.instance.isInAskMode)
        {
            return;
        }
        
        var canvas = FindInParents<Canvas>(gameObject);
        if (canvas == null)
            return;

        // We have clicked something that can be dragged.
        // What we want to do is create an icon for this.
        m_DraggingIcon = gameObject;

        if (dragOnSurfaces)
            m_DraggingPlane = transform as RectTransform;
        else
            m_DraggingPlane = canvas.transform as RectTransform;

        myImage.sprite = Thermometer_PickUp;
        myImage.SetNativeSize();
        isUsingThermometer = true;

    }
    
    public void OnDrag(PointerEventData data)
    {
        if (NPCManager.instance.isInAskMode)
        {
            return;
        }
        
        if (m_DraggingIcon != null)
            SetDraggedPosition(data);
        
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        if (NPCManager.instance.isInAskMode)
        {
            return;
        }
        
        SetDraggedPosition(eventData);
        
        myImage.sprite = Thermometer_OnTheTable;
        myImage.SetNativeSize();
        isUsingThermometer = false;
        isFalling = true;
    }

    static public T FindInParents<T>(GameObject go) where T : Component
    {
        if (go == null) return null;
        var comp = go.GetComponent<T>();

        if (comp != null)
            return comp;

        Transform t = go.transform.parent;
        while (t != null && comp == null)
        {
            comp = t.gameObject.GetComponent<T>();
            t = t.parent;
        }
        return comp;
    }
    
    private void SetDraggedPosition(PointerEventData data)
    {
        if (dragOnSurfaces && data.pointerEnter != null && data.pointerEnter.transform as RectTransform != null)
            m_DraggingPlane = data.pointerEnter.transform as RectTransform;

        var rt = m_DraggingIcon.GetComponent<RectTransform>();
        Vector3 globalMousePos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, data.position, data.pressEventCamera, out globalMousePos))
        {
            Vector3 tempPosition = globalMousePos;
            if (mousePosition.x < ThermometerArea_BottomLeftCorner.position.x)
            {
                tempPosition.x = ThermometerArea_BottomLeftCorner.position.x;
            }
            
            if (mousePosition.x > ThermometerArea_TopRightCorner.position.x)
            {
                tempPosition.x = ThermometerArea_TopRightCorner.position.x;
            }

            if (mousePosition.y < ThermometerArea_BottomLeftCorner.position.y)
            {
                tempPosition.y = ThermometerArea_BottomLeftCorner.position.y;
            }

            if (mousePosition.y > ThermometerArea_TopRightCorner.position.y)
            {
                tempPosition.y = ThermometerArea_TopRightCorner.position.y;
            }
            
            rt.position = tempPosition;
            //rt.position = globalMousePos;
            rt.rotation = m_DraggingPlane.rotation;
        }
    }
    
    public void FreeFallToTheTable()
    {
        Vector3 tempPosition = gameObject.GetComponent<RectTransform>().position;
        tempPosition.y -= fallingSpeed;
        gameObject.GetComponent<RectTransform>().position = tempPosition;
        if (gameObject.GetComponent<RectTransform>().position.y <= stopFallingPositionY)
        {
            isFalling = false;
        }
    }


}
